package cn.wolfcode.wolf2w.cloud.member.feign;

import cn.wolfcode.wolf2w.cloud.feign.UserInfoFeignApi;
import cn.wolfcode.wolf2w.cloud.member.domain.UserInfo;
import cn.wolfcode.wolf2w.cloud.member.exception.MemberCodeMsg;
import cn.wolfcode.wolf2w.cloud.member.service.IUserInfoService;
import cn.wolfode.wolf2w.cloud.commom.exception.BusinessException;
import cn.wolfode.wolf2w.cloud.commom.util.AssertUtil;
import cn.wolfode.wolf2w.cloud.commom.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
//实现feign接口
@RestController
public class UserInfoFeignApiClient implements UserInfoFeignApi {

    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public JsonResult get(String id) {
        return JsonResult.success(userInfoService.get(id));
    }

    @Override
    public JsonResult checkPhone(String phone) {

        return JsonResult.success(userInfoService.checkPhone(phone));
    }




    @Override
    public JsonResult regist(String phone, String nickname, String password, String rpassword) {

//验证操作
        //判断传入参数是否为null
        //断言
        AssertUtil.hasLength(phone, MemberCodeMsg.PHONE_ERROR);
        AssertUtil.hasLength(nickname, MemberCodeMsg.NICKNAME_ERROR);
        AssertUtil.hasLength(password, MemberCodeMsg.PASSWORD_ERROR);
        AssertUtil.hasLength(rpassword, MemberCodeMsg.REPASSWORD_ERROR);
        //判断2次输入的密码是否一致
        AssertUtil.isEquals(password, rpassword, MemberCodeMsg.PASSWORD_EQUAIT_ERROR);
        //判断手机号码是否符合格式 @TODO
        //判断手机号是否唯一
        if(userInfoService.checkPhone(phone)){
            throw new BusinessException(MemberCodeMsg.HAS_REDIST_ERROR);
        }
        //判断验证是否正确




        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setPassword(password); //假装加密
        userInfo.setLevel(1);
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setInfo("");
        //关键字段建议大家自己控制
        userInfo.setState(UserInfo.STATE_NORMAL);
        userInfoService.save(userInfo);
        return JsonResult.success();
    }
}
